package com.scau.dao.Impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.scau.dao.HomeworkDao;
import com.scau.mapper.HomeworkMapper;
import com.scau.pojo.Homework;
import com.scau.util.SqlSessionFactoryUtil;
@Repository
public class HomeworkDaoImpl implements HomeworkDao{
	
	SqlSession session = SqlSessionFactoryUtil.getSession();
	HomeworkMapper homeworkMapper=session.getMapper(HomeworkMapper.class);
	
	@Override
	public List<Homework> getHomeworkBySeId(int id) {
		return homeworkMapper.selectBySectionId(id);
	}

	@Override
	public int countBySeId(int id) {
		return homeworkMapper.countByExample(id);
	}


	public int add(Homework homework) {
		return homeworkMapper.insertSelective(homework);
	}

	public int update(Homework homework) {
		return homeworkMapper.updateByPrimaryKeySelective(homework);
	}

	@Override
	public int delete(int id) {
		return homeworkMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<Homework> queryAllHomework() {
		return homeworkMapper.selectByExample(null);
	}

	@Override
	public int isNameExist(String hName, Integer seId) {
		int result=-1;
		Homework homeworkReturn=new Homework();
		Homework homework=new Homework();
		homework.setSeId(seId);
		homework.sethName(hName);
		try {
			homeworkReturn=homeworkMapper.selectByName(homework);
			if(homeworkReturn!=null){
				result=1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
